package br.com.leilao.cliente.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.leilao.cliente.model.Venda;
import br.com.leilao.util.ConectaBanco;
import br.com.leilao.util.LeilaocodeException;

public class VendaDao {

	private final String INSERT_QUERY = "insert into venda () values (?,?)";
	private final String UPDATE_QUERY = "update venda set tipo = ?, valorTotal = ? where codigo = ?";
	private final String SELECT_ALL_QUERY = "select * from venda";
	private final String SELECT_BY_ID_QUERY = "select * from venda where id = ? ";
	private final String DELETE_BY_ID_QUERY = "delete * from venda where id = ? ";
	
	public void cadastrar(Venda ob) throws LeilaocodeException, ClassNotFoundException {
		Connection con = null;
		PreparedStatement prepStmt = null;
		try {
			con = ConectaBanco.getConnection();
			prepStmt = con.prepareStatement(INSERT_QUERY);
			prepStmt.setString(1, ob.getTipo());
			prepStmt.setDouble(2, ob.getValorTotal());
			prepStmt.execute();
		} catch (SQLException e) {
			String msg = "[ProdutosDB][cadastrar(Venda ob)]: " + e.getMessage();
			LeilaocodeException le = new LeilaocodeException(msg, e);
			throw le;
		} finally {
			ConectaBanco.closeAll(con, prepStmt);
		}
	}

	public void atualizar(Venda ob) throws LeilaocodeException, ClassNotFoundException {
		Connection con = null;
		PreparedStatement prepStmt = null;
		try {
			con = ConectaBanco.getConnection();
			prepStmt = con.prepareStatement(UPDATE_QUERY);
			prepStmt.setString(1, ob.getTipo());
			prepStmt.setDouble(2, ob.getValorTotal());
			prepStmt.setInt(3, ob.getCodigo());
			prepStmt.executeUpdate();
		} catch (SQLException e) {
			String msg = "[ProdutosDB][atualizar(Venda ob)]: " + e.getMessage();
			LeilaocodeException le = new LeilaocodeException(msg, e);
			throw le;
		} finally {
			ConectaBanco.closeAll(con, prepStmt);
		}
	}

	public List<Venda> listar() throws LeilaocodeException, ClassNotFoundException {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		List<Venda> listaVenda = new ArrayList<Venda>();
		try {
			con = ConectaBanco.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(SELECT_ALL_QUERY);
			while (rs.next()) {
				int codigo = rs.getInt("codigo");
				String tipo = rs.getString("tipo");
				double valorTotal = rs.getDouble("valorTotal");
				Venda v = new Venda(codigo, tipo, valorTotal);
				listaVenda.add(v);
			}
		} catch (SQLException e) {
			String msg = "[ProdutosDB][listar()]: "
					+ e.getMessage();
			LeilaocodeException le = new LeilaocodeException(msg, e);
			throw le;
		} finally {
			ConectaBanco.closeAll(con, stmt, rs);
		}
		return listaVenda;
	}
	
	public Venda getVendaId(int codigo) throws LeilaocodeException, ClassNotFoundException{
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		Venda venda = null;
		try {
			con = ConectaBanco.getConnection();
			prepStmt = con.prepareStatement(SELECT_BY_ID_QUERY);
			prepStmt.setInt(1, codigo);
			rs = prepStmt.executeQuery();
			if (rs.next()) {				
				String tipo = rs.getString("tipo");
				double valorTotal = rs.getDouble("valorTotal");
				venda = new Venda(codigo, tipo, valorTotal);
			}
		} catch (SQLException e) {
			String msg = "[ProdutosDB][getVendaId(int codigo)]: " + e.getMessage();
			LeilaocodeException le = new LeilaocodeException(msg, e);
			throw le;
		} finally {
			ConectaBanco.closeAll(con, prepStmt, rs);
		}
		return venda;		
	}
	
	public void excluir(Venda ob) throws LeilaocodeException, ClassNotFoundException, SQLException {
		Connection con = ConectaBanco.getConnection();
		PreparedStatement prepStmt = null;
		try{
			prepStmt = con.prepareStatement(DELETE_BY_ID_QUERY);
			prepStmt.setInt(1, ob.getCodigo());
			prepStmt.execute();
	   }catch(SQLException e){
		   String msg = "[ProdutosDB][excluir(Venda ob)]: " + e.getMessage();
		   LeilaocodeException le = new LeilaocodeException(msg, e);
		   throw le; 
	   }finally {
		   ConectaBanco.closeAll(con, prepStmt);
	   }
	}

}
